package ma.controller.panels;

import java.util.ArrayList;
import java.util.List;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import ma.controller.gui.Controllable;
import ma.controller.panels.auxillary.CtrInformationPanel;
import ma.view.gui.CenterTabbedPane;
import ma.view.gui.CenterTabbedPane.EBenchmarkTab;
import ma.view.gui.EBenchmarkPanel;
import ma.view.gui.ObserverInterface;
import ma.model.EBenchmarkInterface;

/**
 * The {@code CtrCenterTabbedPane} controls the behavior of the main
 * {@code JTabbedPane} of this application.
 * @author Oliver Eckstein
 *
 */
public class CtrCenterTabbedPane implements ChangeListener {
    
    private CenterTabbedPane tabbedPane;
    private EBenchmarkInterface eBenchmark;
    private CtrInformationPanel infoPanelController;
    private ObserverInterface methodsSetupPanel;
    
    /**
     * To create a new instance of a {@code CtrCenterTabbedPane} and connect
     * it to the model and the controller of the information panel.
     * @param eBenchmark the application's model.
     * @param infoPanelController the controller of the information panel.
     */
    public CtrCenterTabbedPane(            
            EBenchmarkInterface eBenchmark,
            CtrInformationPanel infoPanelController) {
        this.eBenchmark = eBenchmark;
        this.infoPanelController = infoPanelController;
        
        addTabs();
        eBenchmark.registerObserver(tabbedPane);                
    }
    
    /**
     * To get the view of this controller.
     * @return the controller's view.
     */
    public CenterTabbedPane getView() {        
        return tabbedPane;
    }
    
    /**
     * To create the tabbed pane and initially add two tabs were the user can
     * choose the number of manufacturing methods to be calculated and set
     * them up. Later on more tabs will be added.
     */
    private void addTabs() {        
        List<EBenchmarkPanel> panels = new ArrayList<EBenchmarkPanel>();
        
        Controllable cm =
                new CtrChooseMethods(
                        eBenchmark,
                        infoPanelController);
        panels.add(cm.getView());        
        
        Controllable sm =
                new CtrSetupMethods(
                        eBenchmark,
                        infoPanelController,
                        this);        
        panels.add(sm.getView());
        
        methodsSetupPanel = sm.getView();
        
        tabbedPane = new CenterTabbedPane(panels, eBenchmark);
        tabbedPane.addChangeListener(this);
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        // Draw the info panel based on the selected tab.
        EBenchmarkTab selectedTab = tabbedPane.getSelectedTab();
        infoPanelController.getView().drawInfoPanel(selectedTab);                  
        
        // To make sure that the methods setup panel only gets updated
        // when it is selected.
        if(selectedTab == EBenchmarkTab.SETUP_METHODS) {            
            eBenchmark.registerObserver(methodsSetupPanel);
            methodsSetupPanel.update();
        } else {
            eBenchmark.removeObserver(methodsSetupPanel);
        }
    }
}
